package com.Thread;

import java.util.concurrent.*;

/**
 * @author: sun.hongliang
 * @create: 2018/11/27 11:13
 */
public class FutureTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        Future<String> future1 = executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("a1");
                TimeUnit.SECONDS.sleep(2);
                System.out.println("a2");
                return "a";
            }
        });

        Future<String> future2 = executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("b1");
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println("11");
                }
                System.out.println("b2");

                return "b";
            }
        });

        Future<String> future3 = executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("c1");
                System.out.println("c2");

                return "b";
            }
        });

        try {
            future1.get(1, TimeUnit.SECONDS);
        } catch (Exception e) {
            future1.cancel(false);
            future2.cancel(true);
            System.out.println("ccc");
            e.printStackTrace();
        }


    }
}
